package com.xayy.mgt.service.impl;

import cn.kawins.base.result.CheckResult;
import cn.kawins.base.utils.BeanUtil;
import cn.kawins.base.utils.StringUtil;
import cn.kawins.mybatis.base.BaseServiceImpl;
import com.xayy.mgt.mapper.NodeInfoMapper;
import com.xayy.mgt.model.NodeTreeInfo;
import com.xayy.mgt.model.SystemTelemetryInfo;
import com.xayy.mgt.service.NodeInfoService;
import com.xayy.mgt.service.SystemTelemetryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Nie
 * @version V1.0
 * @date 2023/10/9 10:00
 */
@Service
public class SystemTelemetryServiceImpl extends BaseServiceImpl<SystemTelemetryInfo> implements SystemTelemetryService {
//    @Autowired
//    private NodeInfoService nodeInfoService;
    @Autowired
    private NodeInfoMapper nodeInfoMapper;

    @Override
    public CheckResult<String> getNodeTelemetryName(Long nodeId) {
        NodeTreeInfo nodeTreeInfo = nodeInfoMapper.selectByPrimaryKey(nodeId);
        if (BeanUtil.isEmpty(nodeTreeInfo))
            return CheckResult.fail("节点信息不存在");
        Long systemId = nodeTreeInfo.getSystemId();
        if (systemId == null)
            return CheckResult.fail("未获取到分系统得节点信息");
        // 查询分系统对应遥测表信息
        try {
            SystemTelemetryInfo systemTelemetryInfo = selectOne(SystemTelemetryInfo.builder().satelliteId(nodeTreeInfo.getSatelliteId()).systemId(systemId).build());
            if (BeanUtil.isEmpty(systemTelemetryInfo) || StringUtil.isBlank(systemTelemetryInfo.getTelemetryTableName()))
                return CheckResult.fail("节点对应分系统未配置遥测表");
            return CheckResult.success(systemTelemetryInfo.getTelemetryTableName());
        } catch (Exception e) {
            return CheckResult.fail("分系统对应遥测表信息配置异常");
        }
    }
}
